package com.zim;

import com.zim.util.SimulationRedisUtil;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

/**
 * 描述:
 *
 * @Author: ZimH
 * @Mobile: 182-5958-1506
 * @CreateDate: 2022/1/10 17:17
 * @Version: 1.0
 */

public class AudioAndVideoConcert_New extends Application {
    private SimulationRedisUtil simulationRedisUtil;
    @Override
    public void start(Stage stage) throws Exception {
        simulationRedisUtil = new SimulationRedisUtil();
        stage.setTitle("音视频转换器");
        stage.setWidth(300);
        stage.setHeight(150);
        stage.setMaxWidth(300);
        stage.setMaxHeight(150);
        stage.setMinWidth(300);
        stage.setMinHeight(150);

        final FileChooser fileChooser = new FileChooser();
        final Button openButton = new Button("MP4转MP3");
        final GridPane inputGridPane = new GridPane();

        GridPane.setConstraints(openButton, 0, 1);
        inputGridPane.setHgap(6);
        inputGridPane.setVgap(6);
        inputGridPane.getChildren().addAll(openButton);

        final Pane rootGroup = new VBox(12);
        rootGroup.getChildren().addAll(inputGridPane);
        rootGroup.setPadding(new Insets(27, 5, 12, 100));

        stage.setScene(new Scene(rootGroup));
        stage.show();

        simulationRedisUtil.set("main",stage);

        openButton.setOnAction(
                (final ActionEvent e) -> {
                    configureFileChooser(fileChooser);
                    //批量选择
                    List<File> files = fileChooser.showOpenMultipleDialog(stage);
                    if (files != null && files.size() > 0) {
                        JOptionPane.showMessageDialog(null, "文件获取成功，开始转换"); //显示提示信息

                        for (int i = 0; i < files.size(); i++) {
                            File file = files.get(i);
                            if (null == simulationRedisUtil.get(file.getName())){
                                //获取文件后缀
                                String sourcePath = file.getPath();
                                String[] split = sourcePath.split("\\.");

                                String suffix = split[1];
                                ProgressDialog view = new ProgressDialog(i + 1,stage.getX(),stage.getY());
                                ConvertingThread_New test = new ConvertingThread_New(file,split[0],suffix,simulationRedisUtil);
                                view.setProgressProperty(test.progressProperty());
                                view.setMessageProperty(test.messageProperty());
                                view.setTitle(file.getName() + "  转换进度");
                                view.show();
                                simulationRedisUtil.set(file.getName(),view);

                                new Thread(test).start();
                                if (0 == i){
                                    stage.close();
                                }
                                view.setOnCloseRequest(event -> {
                                    System.out.println("用户主动点击关闭窗口【" + file.getName() + "】");
                                    simulationRedisUtil.del(file.getName());
                                });
                            }
                        }
                    }
                });
        Platform.setImplicitExit(false); //多次使用显示和隐藏设置false
        SystemTray tray = SystemTray.getSystemTray();
        //此处不能选择ico格式的图片,要使用16*16的png格式的图片
        BufferedImage image = ImageIO.read(this.getClass().getResourceAsStream("/icon/converLog.png"));
        assert image != null;
        TrayIcon trayIcon = new TrayIcon(image, "音视频转换器");
        tray.add(trayIcon);
        trayIcon.addMouseListener(new MouseAdapter() {
            // 鼠标事件
            public void mouseClicked(MouseEvent e) {
                // 鼠标右键关闭
                if(e.getButton() == 3){
                    Platform.exit();
                    System.exit(0); // 必杀退出法
                    return;
                }
                // 判断是否双击了鼠标
                if (e.getClickCount() == 2) {
                    if (stage.isIconified()) {
                        Platform.runLater(() -> {
                            stage.setIconified(false);
                            stage.show();
                        });
                    }
                }
            }
        });

        stage.toBack();
        stage.toFront();
        stage.centerOnScreen();//窗口居中
        stage.setAlwaysOnTop(true);//窗口置顶
        stage.getIcons().add(new Image(this.getClass().getResourceAsStream("/icon/conver.png")));
//        stage.setIconified(true);//自动缩小

        //窗口关闭监听
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                Platform.runLater(() -> {
                    stage.setIconified(true);
                    stage.show();
                });
            }
        });
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

    private static void configureFileChooser(
            final FileChooser fileChooser) {
        fileChooser.setTitle("选择要转换的视频");
        FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
        fileChooser.setInitialDirectory(fsv.getHomeDirectory());
        fileChooser.getExtensionFilters().removeAll();
        fileChooser.getExtensionFilters().add(
                new FileChooser.ExtensionFilter("视频文件","*.mp4")
        );
    }
}
